package com.example.xunua_weather.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.xunua_weather.R;
import com.example.xunua_weather.Sqliteutils.DatabaseBean;
import com.example.xunua_weather.entity.WeatherBean;
import com.google.gson.Gson;

import java.util.List;

public class CityManagerAdapter extends BaseAdapter {
    Context context;
    List<DatabaseBean> mDatas;

    public CityManagerAdapter(Context context, List<DatabaseBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_managelv,null);
            holder=new ViewHolder();
            holder.tv_city=convertView.findViewById(R.id.item_city_tv_locate);
            holder.tv_con=convertView.findViewById(R.id.item_city_tv_con);
            holder.tv_currentTemp=convertView.findViewById(R.id.item_city_tv_temp);
            holder.tv_wind=convertView.findViewById(R.id.item_city_tv_wind);
            holder.tv_tempRange=convertView.findViewById(R.id.item_city_tv_range);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        DatabaseBean bean=mDatas.get(position);
        holder.tv_city.setText(bean.getCity());
        WeatherBean weatherBean=new Gson().fromJson(bean.getContent(),WeatherBean.class);
        WeatherBean.ResultsBean.WeatherDataBean dataBean=weatherBean.getResults().get(0).getWeather_data().get(0);
        holder.tv_con.setText("天气:"+dataBean.getWeather());
        holder.tv_tempRange.setText(dataBean.getTemperature());
        holder.tv_wind.setText(dataBean.getWind());

        String[] split=dataBean.getDate().split("：");//data:      周一 11月18日 (实时：20℃)  这个冒号：注意中英文区分
        String todayTemp = split[1].replace(")","");//去掉20°C） 后边的）符号
        holder.tv_currentTemp.setText(todayTemp+"");
        return convertView;
    }
    private class ViewHolder{
        TextView tv_city,tv_con,tv_currentTemp,tv_tempRange,tv_wind;
    }
}
